/**
 * Created by dell on 2016/10/27.
 */
//输入提示
$(function(){
    $('.input-item input').on('focus',function(){
        if($('label.error').css('display')=='inline-block'){
            return false;
        }else{
            $(this).siblings('span').css('display','block');
        }
    })
        .on('blur',function(){
            $(this).siblings('span').css('display','none');
        });
})
//表单验证
$.validator.setDefaults({
            success: function(e) {
                $(e).siblings('i.fa-check-circle').css('display','inline-block');
            },
            highlight: function(e) {
                    $(e).siblings('i.fa-check-circle').css('display','none');
            },
});
$(function(){
    validdate();
    //检验规则
    function addrules(){
        $.validator.addMethod("stringCheck",
            function(value, element) {
                return this.optional(element) || /^[A-Za-z0-9_\-\u4e00-\u9fa5]+$/.test(value);
            });//只能包括中文字、英文字母、数字和下划线
        $.validator.addMethod('fullNumber', function (value, element) {
            return this.optional(element) || !/^[0-9]+$/.test(
                    value);
        });//用户名不能是纯数字，请重新输入！
        $.validator.addMethod("isMobile",
            function(value, element) {
                var length = value.length;
                var mobile = /^(((1[0-9][0-9]{1})|(15[0-9]{1}))+\d{8})$/;
                return this.optional(element) || (length == 11 && mobile.test(value));
            });//手机号码格式
        $.validator.addMethod('isSpace',function(value,element){
            return this.optional(element)|| /^[^\s'"!@#$%]+$/.test(value)
        })//不含空格和特殊符号、
    }
    function validdate(){
        addrules();
        var e = "<i class='fa fa-minus-circle'></i> ";
        var validator=$('#rgForm').validate({
            submitHandler:function(form){

            },
            rules:{
                username:{
                    stringCheck:true,
                    required:true,
                    minlength:5,
                    fullNumber:true,
                },
                password: {
                    required: true,
                    minlength: 5,
                    isSpace:true,
                },
                confirm_password: {
                    required: true,
                    minlength: 5,
                    equalTo: "#password"
                },
                phone:{
                    required:true,
                    isMobile:true,
                    minlength:11,
                },
                email:{
                    required:true,
                    minlength:4,
                },
                agree:{
                    required:true,
                }
            },
            messages: {
                username: {
                    required: e + "请输入您的用户名",
                    minlength: e + "用户名必须五个字符以上",
                    stringCheck:e+'只能包括中文字、英文字母、数字和下划线',
                    fullNumber:e+'用户名不能是纯数字，请重新输入'
                },
                password: {
                    required: e + "请输入您的密码",
                    minlength: e + "密码必须5个字符以上",
                    isSpace:e+"格式错误"
                },
                confirm_password: {
                    required: e + "请再次输入密码",
                    minlength: e + "密码必须5个字符以上",
                    equalTo: e + "两次输入的密码不一致"
                },
                phone:{
                    required:e+'请输入您的手机号码',
                    isMobile:e+'格式有误',
                },
                email:{
                    required:e+'请输入您的邮箱',
                    email: e + "请输入您的E-mail",
                },
                agree: {
                    required: e + "必须同意协议后才能注册",
                }
            },
        })
    }

})
